package ch.doublet.jw.pubdownloader.config;

import javax.ws.rs.core.UriBuilder;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class ConfigurationBundle {

	private static CompositeConfiguration configuration;

	protected static Configuration getInstance() {

		if (configuration != null) {
			return configuration;
		} else {
			try {
				init();
			} catch (ConfigurationException e) {
				throw new RuntimeException(e);
			}
			return configuration;
		}
	}

	private static void init() throws ConfigurationException {
		CompositeConfiguration config = new CompositeConfiguration();
		config.addConfiguration(new PropertiesConfiguration(
				"application.properties"));
		configuration = config;

	}

	private ConfigurationBundle() {
		// Configuration can't be initialized;
	}

	public static UriBuilder getRepoUri() {
		String base = "jw.repo.config.";
		Configuration instance = getInstance();
		String scheme = instance.getString(base + "scheme");
		String path = instance.getString(base + "path");
		String host = instance.getString(base + "host");
		return UriBuilder.fromPath(path).scheme(scheme).host(host);
	}

}
